<!-- 菜品估清 - 组件 -->
<template>
  <div>
    <div class="countAppraise-wrap">
      <div class="title-box">菜品估清</div>
      <!-- 菜品的滚动列表 -->
      <div class="desktop-scroll-list">
        <div class="scroll-list-item-wrap" v-if="selectActiveFoodList.length">
          <div
            class="scroll-list-item"
            v-for="food in selectActiveFoodList"
            :key="food.id"
            @click.stop="handleSelectFood(food)"
            @dblclick.stop="handleDBAction(food.id)"
          >
            <img
              class="image"
              v-if="!isEmpty(food.images)"
              :src="food.images[0].file.preview_url"
              alt=""
            />
            <!-- 无商品图片的判断 -->
            <div class="not-good-image" v-else>
              <img class="image" src="@/assets/images/home/not-goodimage.png" alt="" />
            </div>
            <div class="desc-box">
              <el-tooltip class="item" effect="dark" :content="food.name" placement="top">
                <div class="name">{{ food.name }}</div>
              </el-tooltip>
              <div class="money" v-if="!isEmpty(food.skuList)">
                ￥{{ food.skuList[0].price }}/份
              </div>
            </div>
            <!-- 已售罄 -->
            <div class="positon-soldOut-box" v-if="food.num === 0">
              <img class="image" src="@/assets/images/home/yishouqing-icon.png" alt="" />
            </div>

            <!-- 显示选中菜品的图标 -->
            <img
              v-if="food.isChecked"
              class="choose-img"
              src="@/assets/images/home/xuanzhongzhuotai.png"
              alt=""
            />
          </div>
        </div>
        <!-- 暂无数据的组件 -->
        <div class="no-data-box" v-else>
          <NotData desc="请在左侧选择估清菜品，选择后此菜品为售完状态" />
        </div>
      </div>
      <div class="desc-btn-box" v-if="selectActiveFoodList.length">
        <div class="desc">
          <span>温馨提示：<br />双击可以进行菜品估清操作哦~</span>
        </div>
        <div class="btn-box">
          <div class="clear-all-btn" @click="handleOpenDialog('current')">清空已选估清</div>
          <div class="clear-all-btn" @click="handleOpenDialog('all')">清空全部估清</div>
        </div>
      </div>
    </div>
    <!-- 选中菜品进行清空估清的弹窗组件 -->
    <FoodClearAppraiseDialog
      v-model:showDialog="showFoodClearAppraiseDialog"
      :actionType="actionType"
      @onConfirm="onConfirm"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElTooltip, ElMessage } from 'element-plus'
import { isEmpty, throttle } from '@/utils/toolsValidate'
import { clearFoodAppraise } from '@/api/appraise'
import FoodClearAppraiseDialog from './FoodClearAppraiseDialog.vue'
import NotData from '@/components/NotData/Index.vue'

interface Props {
  // 已估清的菜品列表
  selectActiveFoodList: any[]
}

const props = defineProps<Props>()
const emit = defineEmits(['onConfirm'])

// 是否显示选中菜品进行清空估清的弹窗组件
const showFoodClearAppraiseDialog = ref(false)
// 操作类型，all是清空全部估清，current是清空所选估清
const actionType = ref('all')
// 当前选中估清进行清空列表
const currentSelectAppraiseFoodList = ref([])

// 选中对应的菜品，进行估清
const handleSelectFood = (datas: any) => {
  console.log(datas)

  // if (datas.num === 0) return
  // 深拷贝处理
  const item = JSON.parse(JSON.stringify(datas))
  const activeIndex = currentSelectAppraiseFoodList.value.findIndex(
    (itm: any) => itm.id === item.id
  )
  if (activeIndex === -1) {
    datas.isChecked = true
    item.isChecked = true
    currentSelectAppraiseFoodList.value.unshift(item)
  } else {
    datas.isChecked = false
    item.isChecked = false
    currentSelectAppraiseFoodList.value.splice(activeIndex, 1)
  }
}

// 双击操作
const handleDBAction = throttle(async (foodId: number) => {
  const datas = {
    //  1:清空选中菜品
    type: 1,
    list: [foodId]
  }
  try {
    const res = await clearFoodAppraise(datas)
    if (res.status === 200) {
      ElMessage.success('操作成功')
      emit('onConfirm')
    }
  } catch (error) {
    console.error('双击清空估清失败:', error)
  }
}, 3000)

// 打开弹窗
const handleOpenDialog = (type: string) => {
  if (currentSelectAppraiseFoodList.value.length === 0 && type === 'current') {
    ElMessage.warning('请先选中对应的菜品')
    return
  }
  actionType.value = type
  showFoodClearAppraiseDialog.value = true
}

// 确认操作
const onConfirm = throttle(async () => {
  const datas = {
    // 0:清空所有售罄菜品  1:清空选中菜品
    type: actionType.value === 'all' ? 0 : 1
  }
  if (actionType.value === 'current') {
    // 选中菜品food_id列表
    datas.list = currentSelectAppraiseFoodList.value.map(item => {
      return item.id
    })
  }
  try {
    const res = await clearFoodAppraise(datas)
    if (res.status === 200) {
      ElMessage.success('操作成功')
      // 关闭弹窗
      showFoodClearAppraiseDialog.value = false
      // 触发父组件刷新
      emit('onConfirm')
    }
  } catch (error) {
    console.error('清空估清失败:', error)
    ElMessage.error('操作失败，请重试')
  }
}, 3000)
</script>

<style lang="scss" scoped>
.countAppraise-wrap {
  // width: 976px;
  min-height: calc(100vh - 108px); // 设置最小高度
  height: 100%;
  background: #ffffff;
  border-radius: 20px;
  box-shadow: 3px 0px 24px 0px rgba(0, 0, 0, 0.16);
  box-sizing: border-box;
  transition: all 1s;
  display: flex;
  flex-direction: column;

  .title-box {
    height: 80px;
    line-height: 80px;
    font-size: 24px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
    border-bottom: 1px solid #f0f0f0;
    flex-shrink: 0; // 防止压缩
  }

  .desktop-scroll-list {
    padding: 0 24px 0 24px;
    flex: 1; // 占据剩余空间
    min-height: 400px; // 设置最小高度
    max-height: calc(100vh - 290px); // 设置最大高度，避免超出屏幕
    box-sizing: border-box;
    overflow-y: auto;
    transition: all 1s;

    .scroll-list-item-wrap {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      justify-content: space-between;
      .scroll-list-item {
        position: relative;
        display: flex;
        align-items: flex-start;
        width: 300px;
        height: 130px;
        background: #f4f4f4;
        border-radius: 12px;
        box-sizing: border-box;
        margin-top: 20px;
        cursor: pointer;
        .image {
          flex-shrink: 0;
          width: 173px;
          height: 130px;
          border-radius: 12px 0 0 12px;
          margin-right: 10px;
        }
        .not-good-image {
          width: 173px;
          height: 130px;
          border-radius: 12px 0 0 12px;
          margin-right: 10px;
          display: flex;
          align-items: center;
          justify-content: center;
          .image {
            flex-shrink: 0;
            width: 124px;
            height: 124px;
          }
        }
        .desc-box {
          padding-top: 12px;
          .name {
            width: 90px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
            font-size: 18px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #30314a;
          }
          .money {
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #333333;
            margin-top: 10px;
          }
        }
      }
      .choose-img {
        position: absolute;
        right: 12px;
        bottom: 12px;
        width: 24px;
        height: 24px;
        z-index: 2;
      }
      // 已售罄
      .positon-soldOut-box {
        position: absolute;
        left: 0;
        top: 0;
        width: 300px;
        height: 130px;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 1;
        .image {
          flex-shrink: 0;
          width: 142px;
          height: 117px;
        }
      }
    }
  }
  .scroll-list-item-wrap::after {
    content: '';
    width: 300px;
  }
  //滚动条整体的大小
  .desktop-scroll-list::-webkit-scrollbar {
    width: 6px;
    height: 0;
  }
  //滚动条里滑动的滑块
  .desktop-scroll-list::-webkit-scrollbar-thumb {
    height: 200px;
    background: #d2d2d2;
    border-radius: 3px;
  }
  //滚动条底层样式
  .desktop-scroll-list::-webkit-scrollbar-track {
    display: none;
  }
  .desc-btn-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    // width: 976px;
    height: 92px;
    background: #ffffff;
    border-radius: 0px 0px 20px 20px;
    box-shadow: 0px -4px 6px 0px rgba(0, 0, 0, 0.04);
    box-sizing: border-box;
    padding: 0 30px;
    flex-shrink: 0; // 防止压缩
    .desc {
      font-size: 16px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      color: #333333;
      justify-self: start;
    }
    .btn-box {
      display: flex;
      align-items: center;
      gap: 20px;
      .clear-all-btn {
        width: 154px;
        height: 50px;
        background: #ffffff;
        border: 1px solid #333333;
        border-radius: 12px;
        line-height: 50px;
        font-size: 18px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: center;
        color: #333333;
        cursor: pointer;
      }
      .clear-all-btn:hover {
        color: #7c0a03;
        border-color: #7c0a03;
      }
    }
  }
}
.no-data-box {
  margin-top: 200px;
}
</style>
